<template>
    <div class="user-layout">
        <!-- 头部组件 -->
        <headerIndex></headerIndex>

        <div class="main-container">
            <el-container>
                <!-- 侧边栏导航 -->
                <el-aside width="220px" class="sidebar">
                    <div class="user-info">
                        <el-avatar :size="64" :src="avatar" class="user-avatar">
                            <img src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
                        </el-avatar>
                        <h3 class="username">{{ username }}</h3>
                    </div>

                    <el-menu :default-active="activeMenu" class="el-menu-vertical" router @select="handleSelect">
                        <el-menu-item index="/user/my">
                            <el-icon><HomeFilled /></el-icon>
                            <span>个人中心</span>
                        </el-menu-item>
                        <el-menu-item index="/user/info">
                            <el-icon><UserFilled /></el-icon>
                            <span>个人信息</span>
                        </el-menu-item>
                        <el-menu-item index="/user/resume">
                            <el-icon><Document /></el-icon>
                            <span>我的简历</span>
                        </el-menu-item>
                        <el-menu-item index="/user/submitResume">
                            <el-icon><Promotion /></el-icon>
                            <span>投递历史</span>
                        </el-menu-item>
                        <el-menu-item index="/user/collect">
                            <el-icon><Star /></el-icon>
                            <span>我的收藏</span>
                        </el-menu-item>
                    </el-menu>
                </el-aside>

                <!-- 主内容区 -->
                <el-main class="main-content">
                    <div class="content-header">
                        <el-breadcrumb separator="/">
                            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                            <el-breadcrumb-item>{{ pageTitle }}</el-breadcrumb-item>
                        </el-breadcrumb>
                    </div>

                    <div class="content-body">
                        <router-view v-slot="{ Component }">
                            <keep-alive>
                                <component :is="Component" />
                            </keep-alive>
                        </router-view>
                    </div>
                </el-main>
            </el-container>
        </div>

        <!-- 底部组件 -->
        <footerIndex></footerIndex>
    </div>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import store from "@/store";
import headerIndex from "@/views/index/components/headerIndex.vue";
import footerIndex from "@/views/index/components/footerIndex.vue";
// 导入图标工具
import { HomeFilled, UserFilled, Document, Promotion, Star } from "@/utils/icons.js";
// 导入消息工具
import message from "@/utils/message.js";

const route = useRoute();
const router = useRouter();

// 当前激活的菜单项
const activeMenu = ref(route.path);

// 根据路由获取页面标题
const pageTitle = computed(() => {
    const routeMap: Record<string, string> = {
        "/user/my": "个人中心",
        "/user/info": "个人信息",
        "/user/resume": "我的简历",
        "/user/submitResume": "投递历史",
        "/user/collect": "我的收藏",
    };
    return routeMap[route.path] || "用户中心";
});

// 用户信息
const userInfo = computed(() => store.state.gdepUserInfo || {});
const avatar = computed(
    () => userInfo.value.avatar || "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"
);
const username = computed(() => userInfo.value.user_name || userInfo.value.account || "用户");

// 处理菜单选择
const handleSelect = (key: string) => {
    activeMenu.value = key;
};

// 监听路由变化，更新激活的菜单项
watch(
    () => route.path,
    (newPath) => {
        activeMenu.value = newPath;
    }
);

onMounted(() => {
    // 如果没有登录信息，重定向到登录页
    if (!userInfo.value || Object.keys(userInfo.value).length === 0) {
        router.push({
            path: "/login",
        });
    }
});
</script>

<style scoped>
.user-layout {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow-x: hidden; /* 防止水平滚动条 */
}

.main-container {
    flex: 1;
    max-width: 1200px;
    margin: 20px auto; /* 增加顶部间距 */
    padding: 0 20px;
    width: 100%;
    box-sizing: border-box; /* 确保padding不会增加总宽度 */
    overflow: visible; /* 允许内容正常流动 */
}

.el-container {
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    overflow: hidden;
    min-height: calc(100vh - 180px); /* 减去头部和底部高度以及间距 */
}

.sidebar {
    background-color: #f9f9f9;
    border-right: 1px solid #eee;
    padding: 0;
    height: auto; /* 自动适应高度 */
    position: sticky; /* 使侧边栏在滚动时保持位置 */
    top: 0;
}

.user-info {
    padding: 20px 0;
    text-align: center;
    border-bottom: 1px solid #eee;
}

.user-avatar {
    margin: 0 auto 10px;
    border: 2px solid #eee;
}

.username {
    margin: 10px 0 0;
    font-size: 16px;
    color: #333;
}

.el-menu-vertical {
    border-right: none;
}

.el-menu-item {
    height: 50px;
    line-height: 50px;
}

.main-content {
    padding: 0;
    overflow-y: auto; /* 在内容区域内滚动 */
    height: 100%; /* 确保内容区占满容器高度 */
    max-height: calc(100vh - 180px); /* 限制最大高度 */
}

.content-header {
    padding: 15px 20px;
    background-color: #f9f9f9;
    border-bottom: 1px solid #eee;
    position: sticky; /* 使面包屑导航在滚动时保持位置 */
    top: 0;
    z-index: 1;
}

.content-body {
    padding: 20px;
    overflow-y: auto; /* 允许内容滚动 */
}

/* 响应式设计 */
@media (max-width: 768px) {
    .main-container {
        padding: 10px;
        margin: 10px auto; /* 减少移动端间距 */
    }

    .el-container {
        flex-direction: column;
        min-height: calc(100vh - 140px); /* 调整移动端高度 */
    }

    .el-aside {
        width: 100% !important;
        position: relative; /* 移动端不使用sticky定位 */
    }

    .main-content {
        max-height: none; /* 移动端不限制最大高度 */
    }

    .el-menu--horizontal {
        display: flex;
        width: 100%;
        overflow-x: auto;
    }

    .el-menu-item {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        flex: 1;
    }
}
</style>
